package com.nf.health.app.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;

import com.nf.health.app.R;
import com.nf.health.app.utils.DensityUtil;

/**
 * Created by Administrator on 2017/2/17/017.
 */

public class LineGridView extends GridView {
        /**
         * 默认的分割线的颜色
         * 也可以在布局中设置
         */
        private int diverColor = R.color.color1;
        /**
         * 默认的分割线的高度
         * 也可以在布局中设置
         */
        private int diverHeight = 1;
        /**
         * 所使用的画笔
         */
        private Paint paint;

        private  Context context;
        public LineGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context =context;
            TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.gv_acusttrs);
            diverHeight = (int) typedArray.getDimension(R.styleable.gv_acusttrs_divierHeight,10);
            diverColor = typedArray.getResourceId(R.styleable.gv_acusttrs_divierColor,R.color.black4);
            typedArray.recycle();
            paint = new Paint();
            paint.setColor(ContextCompat.getColor(context,diverColor));
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(diverHeight);
        }
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        }
        /**
         * 动态修改默认的分割线的颜色
         */
        public void setDiverColor(int diverColor){
            this.diverColor = diverColor;
            invalidate();
        }
        /**
         * 动态修改默认的分割线的颜色
         */
        public void setDiverHeight(int diverHeight){
            this.diverHeight = diverHeight;
            invalidate();
        }
        /**
         *
         * @param canvas
         */

  /*  @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = new Rect();
        rect.left = DensityUtil.getScreenIntWidth(context) / 4;
        rect.top = DensityUtil.getScreenIntHeight(context) / 4;
        rect.right = DensityUtil.getScreenIntWidth(context)/ 4 * 3;
        rect.bottom = DensityUtil.getScreenIntHeight(context)/ 4 * 3;
        canvas.drawRect(rect,paint);
    }*/
        @Override
        protected void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
            View localView1 = getChildAt(0);//得到第一个view
            int column = getWidth() / localView1.getWidth();//列数
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View cellView = getChildAt(i);
                if ((i + 1) % column == 0) {//每一行最后一个
                    canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), paint);
                } else if ((i + 1) > (childCount - (childCount % column))) {//最后一行的item
                    canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), paint);
                    canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), paint);
                } else {
                    canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), paint);
                    canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), paint);
                }
            }
        }
}
